<template>
  <div class="parent">
    <echartsDom v-if="isShowEcharts" style="width: 100%; height: 100%" :width="width" :height="height" :option="option" :type="type" />
    <div v-else>
      <px-empty :image-size="50" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, watch } from "vue";
import echartsDom from "./echartsDom.vue";
import { queryConsumeChartForDay } from "@/api/waterKanban";
import * as echarts from "echarts";
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  color: {
    type: String,
    default: "#3B82F6"
  },
  type: {
    type: String,
    default: ""
  },
  chartvalue: {
    type: Object,
    required: true
  }
});
const isShowEcharts = ref(false);
const state = reactive({
  option: {}
});
const { option } = toRefs(state);
const getoptions = () => {
  console.log(props.chartvalue);
  if (props.chartvalue != null && props.chartvalue.yaxis) {
    isShowEcharts.value = true;
    const series = props.chartvalue.yaxis.map(item => {
      return {
        data: item.value,
        type: "bar",
        name: item.name,
        // barWidth: "25px",
        label: {
          show: false,
          color: props.type == "dark" ? "white" : "black"
        },
        emphasis: {
          focus: "series"
        }
      };
    });
    option.value = {
      backgroundColor: props.type == "dark" ? "#001e3b" : "",
      grid: {
        left: "10px",
        top: "55px",
        right: "0px",
        bottom: "10px",
        containLabel: true
      },
      legend: {
        x: "center",
        type: "scroll",
        width: "80%"
      },
      tooltip: {
        confine: true,
        trigger: "axis"
      },
      xAxis: {
        type: "category",
        boundarGap: false,
        data: props.chartvalue.xaxis
      },
      yAxis: {
        name: "tCO2e",
        type: "value"
      },
      series: series
    };
  } else {
    isShowEcharts.value = false;
  }
};
watch(
  () => props.chartvalue,
  newOption => {
    console.log(newOption);
    getoptions();
  },
  { immediate: true }
);
</script>

<style scoped lang="scss">
.parent {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
